$MEmuHomePath = "D:\Tools\Microvirt\MEmu"
$MEmuAdbPath = $MEmuHomePath + "\adb.exe"
$MEmuConsolePath = $MEmuHomePath + "\MEmuConsole.exe"
$AndroidSdkHomePath = "D:\Tools\Android\Sdk\platform-tools"
$AndroidSdkAdbPath = $AndroidSdkHomePath + "\adb.exe"

Write-Output "> Start MEmu Engine"
Start-Process $MEmuConsolePath "MEmu"


Write-Output "> Restart ADB Server"
Start-Process $AndroidSdkAdbPath "kill-server" -NoNewWindow -Wait 
Start-Process $AndroidSdkAdbPath "start-server" -NoNewWindow | Out-Null

Write-Output "> Try to connect MEmu Engine"
$output = ""
$count = 0
while ($output.IndexOf("already connected to") -lt 0) {
    Start-Sleep 1
    $count = $count + 1
    Write-Output ("> Reconnection ( " + $count + " )...")
    $output = & $AndroidSdkAdbPath connect 127.0.0.1:21503 | Out-String
}
Write-Output "> Done."
